<%@tag import="by.epam.communalservices.entity.User"%>
<%@tag import="by.epam.communalservices.utils.Login"%>
<%@tag import="java.sql.Connection"%>

<%@ attribute name="user" required="true" type="by.epam.communalservices.entity.User"%>
<%@ attribute name="dif"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/connect" prefix="connect"%>

<connect:rule val="admin" rules="admin" />
<connect:rule val="editor" rules="admin,edit,user_edit" />

<%
    Login login = new Login((Connection) request.getSession().getAttribute("currentConnection"));
    request.setAttribute("roles", login.getRole(((User) getJspContext().getAttribute("user")).getId()));
%>

<c:choose>
	<c:when test="${editor}">
		<c:set var="link" value='"user_edit?id=${user.id}"' />
		<c:set var="click" value=" onclick='document.location.href=${link}' style='cursor:hand;'" />
	</c:when>
	<c:otherwise>
		<c:set var="click" value="" />
	</c:otherwise>
</c:choose>

<tr onmouseover="this.backup=this.style.backgroundColor;this.style.backgroundColor='red';"
	onmouseout="this.style.backgroundColor=this.backup;" id="${dif}">
	<td ${click}><c:out value="${user.id}" />
	</td>
	<td ${click}><c:out value="${user.login}" />
	</td>
	<c:choose>
		<c:when test="${admin}">
			<td ${click}><c:out value="${user.passw}" />
			</td>
		</c:when>
		<c:otherwise>
			<td ${click}>***</td>
		</c:otherwise>
	</c:choose>
	<td ${click}>
		<c:forEach items="${roles}" var="role">
			<c:out value="${role}" />
		</c:forEach>
	</td>
	<td id='l'>
		<c:choose>
		<c:when test="${editor}">
			<button type="button"
				onclick="document.location.href='user_edit?id=${user.id}'">edit</button>
			<button type="button"
				onclick="if (confirm('Уверены?')) document.location.href='user_edit?delete=${user.id}'"
				style="color: red;">delete</button>
		</c:when>
		<c:otherwise>
			только просмотр
		</c:otherwise>
		</c:choose>
	</td>
</tr>